//# sourceURL=http://uMapApp/Module/RoadLine/RoadLine
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth",
    "../../UI/Layer",
    "../../Earth/ViewPort",
    "../StakeNum/StakeNum"
], function (
    mhjs,
    uMap,
    uEarth,
    Layer,
    ViewPort,
    StakeNum
) {

    function RoadLine(option) {
        var _this = this;
        option && _this.OptionToProperties(option);

        // _this.positions = new uMap.Positions(_this["位置"]);
        //
        // _this.startStakeNum = StakeNum.transform(_this["开始桩号"]);
        // _this.stopStakeNum = StakeNum.transform(_this["结束桩号"]);

        _this.title = _this["朝向"] + " / " + _this["开始桩号"] + "-" + _this["结束桩号"] + " / " + _this["车道线序号"];

        _this.searchKey = _this["朝向"] + " " + _this["开始桩号"] + " " + _this["结束桩号"] + " " + _this["车道线序号"];

        _this.sortKey = (_this["朝向"] === "上行" ? 1e7 : 2e7) + _this.startStakeNum.distance + _this["车道线序号"] * 0.1;

        _this.startPoint = _this.positions.positions[0];
        _this.endPoint = _this.positions.positions[_this.positions.positions.length - 1];

        _this.layer = new Layer({
            "app"     : _this.app,
            "viewport": new ViewPort({
                "center": _this.positions.positions[2],
                "hpr"   : {
                    "heading": uMap.Positions.getHeading(_this.positions.positions[0], _this.positions.positions[_this.positions.positions.length - 1]),
                    "pitch"  : -20,
                    "range"  : 600
                }
            })
        });

        // _this.entity = new uMap.Entity({
        //     objectOption: {
        //         polyline: new uMap.PolylineGraphic({
        //             objectOption: {
        //                 positions    : _this.positions.positions,
        //                 clampToGround: true,
        //                 width        : 4,
        //                 color        : ["#FF0"]
        //             }
        //         })
        //     }
        // });
        //
        // _this.layer.dataSources.entities.add(_this.entity.object);

        _this.polyline = new uMap.GroundPolylinePrimitive({
            positions: _this.positions,
            color    : ["#FF0"],
            width    : 4
        });

        _this.layer.primitives.add(_this.polyline.object);

        _this.pointLayer = new Layer({
            "app": _this.app
        });

        _this.pointLayer.close();

        $.each(_this.positions.positions, function (i, position) {
            _this.pointLayer.dataSources.entities.add(new uMap.Entity({
                objectOption: {
                    position: position,
                    point   : new uMap.PointGraphic(),
                    label   : new uMap.LabelGraphic({
                        objectOption: {
                            text                    : i + 1,
                            verticalOrigin          : "BOTTOM",
                            pixelOffset             : [0, -10],
                            disableDepthTestDistance: Number.POSITIVE_INFINITY
                        }
                    })
                }
            }).object);
        });

    }

    var _class = RoadLine;

    mhjs.OptionToPropertiesInterface(_class);
    mhjs.EventInterface(_class);

    Object.defineProperties(_class.prototype, {
        "open"  : {
            value: function () {
                if (!this.isOpened) {
                    this.layer && this.layer.open();
                    this.isOpened = true;
                    this.trigger("open");
                }
            }
        },
        "close" : {
            value: function () {
                if (this.isOpened) {
                    this.layer && this.layer.close();
                    this.pointLayer && this.pointLayer.close();
                    this.isOpened = false;
                    this.trigger("close");
                }
            }
        },
        "toggle": {
            value: function () {
                if (this.isOpened) {
                    this.close();
                } else {
                    this.open();
                }
            }
        },
        "active": {
            value: function (isActive) {
                var _this = this;

                isActive = (isActive === undefined) || Boolean(isActive);
                _this.isActive = _this.isActive === undefined ? false : _this.isActive;

                if (isActive !== _this.isActive) {

                    if (isActive) {
                        // _this.layer.active();
                        _this.pointLayer.open();
                        _this.polyline.color = new uMap.Color(["#F00"]);
                        _this.trigger("active");

                        // var measure = new uMap.MeasureLines(_this.positions);
                        // console.log(measure.distance);
                        // console.log(measure.surfaceDistance);

                    } else {
                        _this.pointLayer.close();
                        _this.polyline.color = new uMap.Color(["#FF0"]);
                        _this.trigger("unActive");
                    }

                    _this.isActive = isActive;

                }

            }
        }
    });

    return _class;

});